package mlz.b_collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//2.定义一个房间类，初始化房间的时候进行准备54张牌
public class Room {
    //定义一副牌 -》一共54张
    private List<Card> list = new ArrayList<>();

    //初始化54张牌
    public Room() {
        //1.往list集合中添加元素
        //a.点数 个数确定了,类型确定
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //b.花色 个数确定了 类型确定
        String[] colors = {"♣","♠","♥","♦"};

        //表示每张牌的大小
        int size = 0;

        //遍历点数 和 花色,然后进行组合
        for(String number : numbers){
            // number: 3
            size++;
            for(String color : colors){
                //color = "♣"
                //得到的是一张牌
                Card card = new Card(number, color,size);
                list.add(card);//将生成的牌添加到集合中
            }
        }

        //单独存入大小王
        Card c1 = new Card("", "大王",size++);
        Card c2 = new Card("", "小王",size++);
        Collections.addAll(list,c1,c2);

        System.out.println(list.size());
        System.out.println(list);
    }


    //定义开始游戏的方法
    public void start(){
        //1.洗牌 list进行洗牌
        Collections.shuffle(list);
        System.out.println("洗牌后:" + list);

        //2.发牌 -> 3个玩家 17张牌 3张牌是底牌
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        for (int i = 0; i < list.size() - 3; i++) {
            //获取集合中所有的元素
            Card c = list.get(i);
            //判断发牌给谁
            if (i % 3 == 0){
                player1.add(c);
            }else if (i % 3 == 1){
                player2.add(c);
            } else if (i % 3 == 2) {
                player3.add(c);
            }
        }

        //3.对玩家的牌进行排序
        sortCard(player1);
        sortCard(player2);
        sortCard(player3);

        //4.玩家看牌
        System.out.println("玩家1:" + player1);
        System.out.println("玩家2:" + player2);
        System.out.println("玩家3:" + player3);
        //还有3张底牌
        List<Card> cards = list.subList(list.size() - 3, list.size());
        System.out.println("底牌:" + cards);
    }

    //集中排序
    private void sortCard(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize() - o1.getSize();
            }
        });
    }



}














